package com.ihomey.cc.library.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;

/**
 * Created by Administrator on 2017/5/9.
 */

public class FileUtil {

    public static File getAppSubDir(String dir) {
        File appSubDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + dir);
        if (!appSubDir.exists()) {
            appSubDir.mkdirs();
        }
        return appSubDir;
    }

    public static void saveImageToGallery(Context context,Bitmap bmp,String path) {
        File imageExportDir = FileUtil.getAppSubDir(path);
        String fileName = System.currentTimeMillis() + ".png";
        File file = new File(imageExportDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
            Toast.makeText(context, "图片已保存至" + imageExportDir.getAbsolutePath() + "文件夹", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(context, "保存出错了...", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }

}
